//
//  ShellForgetViewController.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/1.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellForgetViewController.h"
#import "ShellAccountTextField.h"
#import "ShellAccountViewModel.h"


@interface ShellForgetViewController ()

@property (nonatomic,strong) UIButton *verificationBtn;

//取消订阅RAC信号
@property (nonatomic,strong) RACDisposable *disposable;
@property (nonatomic,assign) NSInteger timeCount;
@property (nonatomic,strong) ShellAccountViewModel *viewModel;


@end

@implementation ShellForgetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = LZLocalizedString(@"忘记密码");
    
    [self setupView];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = COLOR_TINT;
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //查看堆栈, 看是否有该VC
    NSArray * navs = self.navigationController.viewControllers;
    BOOL isDispose = YES;
    for (UIViewController * vc in navs){
        NSString * VCString =  NSStringFromClass([vc class]);
        NSString * orderVCString =  NSStringFromClass([self class]);
        if([VCString isEqualToString:orderVCString]){
            isDispose = NO;
        }
    }
    if(isDispose) {
        //释放定时器
        [self.disposable dispose];
        self.disposable = nil;
    }
}


- (void)setupView{
    
    ///!!!:邮箱
    ShellAccountTextField *accountTextField = [[ShellAccountTextField alloc] init];
    accountTextField.placeholder = LZLocalizedString(@"邮箱");
    accountTextField.tag = 2019;
    [self.view addSubview:accountTextField];
    [accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(40);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    
    ///!!!:邮箱验证码
    ShellAccountTextField *codeTextField = [[ShellAccountTextField alloc] init];
    codeTextField.placeholder = LZLocalizedString(@"邮箱验证码");
    [self.view addSubview:codeTextField];
    [codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(accountTextField.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    self.verificationBtn.frame = CGRectMake(0, 0, 110, 50);
    codeTextField.rightView = self.verificationBtn;
    codeTextField.rightViewMode = UITextFieldViewModeAlways;
    
    ///!!!:新密码
    ShellAccountTextField *passwordTextField = [[ShellAccountTextField alloc] init];
    passwordTextField.secureTextEntry = YES;
    [self.view addSubview:passwordTextField];
    passwordTextField.placeholder = LZLocalizedString(@"新密码");
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(codeTextField.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    
    ///!!!:重新输入新密码
    ShellAccountTextField *surePasswordTextField = [[ShellAccountTextField alloc] init];
    surePasswordTextField.secureTextEntry = YES;
    [self.view addSubview:surePasswordTextField];
    surePasswordTextField.placeholder = LZLocalizedString(@"重新输入新密码");
    [surePasswordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passwordTextField.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    
    
    ///!!!:提交
    UIButton *submitBtn = [UIButton lz_buttonTitle:LZLocalizedString(@"提交") titleColor:UIColorHex(0xFFFFFF) fontSize:17];
    [submitBtn setGradientBackgroundWithColors:@[UIColorHex(0x65DFFE),UIColorHex(0x2C6FE)] locations:@[@(0), @(1.0f)] startPoint:CGPointMake(1, 0.5) endPoint:CGPointMake(0, 0.5)];
    [self.view addSubview:submitBtn];
    submitBtn.cornerRadius = 6;
    submitBtn.layer.shadowOffset = CGSizeMake(0,2);
    submitBtn.layer.shadowOpacity = 1;
    submitBtn.layer.shadowRadius = 6;
    submitBtn.enabled = NO;
    [submitBtn setTitleColor:RGBA(255, 255, 255, 0.5) forState:UIControlStateDisabled];
    [submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(surePasswordTextField.mas_bottom).mas_offset(26);
        make.left.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(50);
    }];
    
    
    
    
  
    //提交按钮的控制
    [[codeTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        if(accountTextField.text.length > 6
           && passwordTextField.text.length > 5
           && [passwordTextField.text isEqualToString:surePasswordTextField.text]
           && codeTextField.text.length > 3){
            submitBtn.enabled = YES;
        } else {
            submitBtn.enabled = NO;
        }
    }];


    [[passwordTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        if(accountTextField.text.length > 6
           && passwordTextField.text.length > 5
           && [passwordTextField.text isEqualToString:surePasswordTextField.text]
           && codeTextField.text.length > 3){
            submitBtn.enabled = YES;
        } else {
            submitBtn.enabled = NO;
        }
    }];




    [[surePasswordTextField rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        if(accountTextField.text.length > 6
           && passwordTextField.text.length > 5
           && [passwordTextField.text isEqualToString:surePasswordTextField.text]
           && codeTextField.text.length > 3){
            submitBtn.enabled = YES;
        } else {
            submitBtn.enabled = NO;
        }
    }];
    
    
    @weakify(self);
    [[submitBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        ///!!!:提交
        @strongify(self);
        [self.viewModel forgetAccount:accountTextField.text verification:codeTextField.text password:passwordTextField.text];

    }];
    
    
    ///回调
    [self.viewModel.reloadSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self actionType:[x integerValue]];
    }];
    
}

///处理逻辑问题
- (void)actionType:(NSInteger)type{
    
    switch (type) {
        case 1:{
            [self startDisposable];
        }
            break;
            
        default:
            break;
    }
    
    
}
//发送验证码
- (void)verificationCodeClick:(UIButton *)btn{
    ShellAccountTextField *accountTextField = [self.view viewWithTag:2019];
    if(accountTextField.text.length == 0)return;
    [self.viewModel sendVerifiCode:accountTextField.text type:2];
}

//开启倒计时
- (void)startDisposable {
    
    @weakify(self)
    [self.view endEditing:YES];
    self.timeCount = K_timeCount;
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.verificationBtn setTitle:[NSString stringWithFormat:@"%ldS",(long)self.timeCount] forState:self.timeCount > 0 ? UIControlStateDisabled:UIControlStateNormal];
    });
    
    self.verificationBtn.enabled = NO;
    
    //button type要 设置成custom 否则会闪动
    self.disposable = [[RACSignal interval:1.0 onScheduler:[RACScheduler mainThreadScheduler]]subscribeNext:^(NSDate * _Nullable x) {
        @strongify(self)
        //时间 --
        NSLog(@"发送验证码 %ld",(long)self.timeCount);
        
        self.timeCount -- ;
        if(self.timeCount < 1){
            self.verificationBtn.enabled = YES;
            self.timeCount  = 0;
            [self.disposable dispose];
            self.disposable = nil;
        } else {
            self.verificationBtn.enabled = NO;
        }
        //设置文字
        NSString * btnText = self.timeCount > 0 ? [NSString stringWithFormat:@"%ldS",(long)self.timeCount]:[NSString stringWithFormat:@"%@",LZLocalizedString(@"重新发送")];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            @strongify(self)
            self.verificationBtn.frame = CGRectMake(0, 0, 110, 50);
            [self.verificationBtn setTitle:btnText forState:self.timeCount > 0 ? UIControlStateDisabled:UIControlStateNormal];
        });
    }];
    
}

- (UIButton *)verificationBtn{
    if(!_verificationBtn){
        _verificationBtn = [[UIButton alloc] init];
        [_verificationBtn setTitle:[NSString stringWithFormat:@"%@",LZLocalizedString(@"获取验证码")] forState:UIControlStateNormal];
        [_verificationBtn setTitleColor:UIColorHex(0x4AB2E7) forState:UIControlStateNormal];
        _verificationBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [_verificationBtn addTarget:self action:@selector(verificationCodeClick:) forControlEvents:UIControlEventTouchUpInside];
        _verificationBtn.enabled = YES;
    }
    return _verificationBtn;
}



//viewModel;
- (ShellAccountViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[ShellAccountViewModel alloc] init];
    }
    return _viewModel;
}


@end
